package com.cdfahe.gms.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cdfahe.common.Constants;
import com.cdfahe.common.util.mail.Mail;
import com.cdfahe.common.util.mail.MailSender;
import com.cdfahe.common.util.mail.MailUtils;
import com.cdfahe.gms.mybatis.entity.News;
import com.cdfahe.gms.service.INewsService;

/**
 * @author Donzell
 * @version 1.0
 * 走进法和栏目下的各个页面控制器
 */
@Controller("aboutController")
public class AboutController {
	
	@Autowired
	private INewsService newsService;
	
//	@RequestMapping(value = "/ajaxTest")
//    public String ajaxTest(Model model,HttpServletRequest request) {
//		String picid = request.getParameter("picid");
//		System.out.println(picid);
//		return "删除成功！";
//    }
	
	/**
	 * 产品目录模块控制器
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/productdir")
    public String productDir(Model model,HttpServletRequest request) {
		News productDirNews = newsService.getNewsById(Integer.parseInt(request.getParameter("id")));
//		if (productDirNews.getCategoryid().equals("6") && productDirNews.getNewstype().equals("0")) {
//			
//		} else {
//			productDirNews = newsService.getNewsById(61);
//		}
		model.addAttribute("content", productDirNews.getContent());
		model.addAttribute("newsid", productDirNews.getId());
		model.addAttribute("newstitle", productDirNews.getName());
		return "about/productdir";
    }
	
	/** 
	 * 联系我们模块控制器
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/contactus")
    public String contactUs(Model model,HttpServletRequest request) {
		News windrugsNews = newsService.getNewsById(51);
		model.addAttribute("content", windrugsNews.getContent());
		return "about/contactus";
    }
	
	/**
	 * 配送渠道模块控制器
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/cooperation")
    public String Cooperation (Model model,HttpServletRequest request) {
		News  news = newsService.getNewsById(Integer.parseInt(request.getParameter("id")));
		model.addAttribute("newsid", request.getParameter("id"));
		model.addAttribute("newstitle", news.getName());
		model.addAttribute("content", news.getContent());
		return "about/cooperation";
    }
	
	@RequestMapping(value = "/about")
    public String getStaticPage(Model model,HttpServletRequest request) {
		News  news = null;
		int newsid = Integer.parseInt(request.getParameter("id"));
		if (newsid >= 1 && newsid <= 5) {
			news = newsService.getNewsById(newsid);
		}else{
			news = newsService.getNewsById(1);
		}
		model.addAttribute("newstitle", news.getName());
		model.addAttribute("newsid", news.getId());
		model.addAttribute("newscontent", news.getContent());
		return "about/about";
    }
	
	@RequestMapping(value = "/test")
	public String test(Model model,HttpServletRequest request) throws Exception{
		MailUtils mu = new MailUtils();
		MailSender mt = new MailSender();
		try {
			System.setProperty("javax.net.debug", "ssl"); //打开调试信息
			Mail mail = mu.getMail("1");
			System.out.println(mail.getSubject()+"===112223333333333");
			String ct = mail.getContent();
			StringBuilder sb = new StringBuilder(ct);
			mu.replaceParam(sb, "这里", "<a href='http://www.baidu.com'/>这里</a>");
			sb.append("<h1>这里是标题1</h1><br>");
			sb.append("<h2>这里是标题2</h2>");
			sb.append("<a href="+"HTTP://www.baidu.com"+">这里是超链接</a>");
			System.out.println("替换后"+sb.toString());
			mail.setContent(sb.toString());
//			String content = MailSender.readFileByLines(MailUtils.MAIL_CONTENT_WEL);
//			System.out.println("文件读取的邮件内容"+content);
//			mail.setContent(content);
			
			mail.setAttached(true);
			mail.addAttached("附件1.zip", MailUtils.MAIL_FILE);
			mt.sendMail(mail,true); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return null;
	}
}
